package com.orchid.community.models.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.ApplyDTO;
import com.orchid.community.models.DTO.MissionDto;
import com.orchid.community.models.DTO.MissionPageParam;
import com.orchid.community.models.DTO.MissionRequestDTO;
import com.orchid.community.models.entity.Apply;
import com.orchid.community.models.entity.Mission;
import com.orchid.community.models.mapper.MissionMapper;
import com.orchid.community.models.service.MissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MissionServiceImpl implements MissionService {

    @Autowired
    private MissionMapper missionMapper;


    @Override
    public MsgResponse getSceneTags() {
        List<String> sceneTags = missionMapper.getSceneTags();
        return ResultUtils.success(sceneTags);
    }

    @Override
    public MsgResponse addMission(Map<String, Object> map) {
        String title = map.get("title").toString();
        String begin = map.get("begin").toString();
        String end = map.get("end").toString();
        String place = map.get("place").toString();
        String scene = map.get("scene").toString();
        String qrcode = map.get("qrcode").toString();
        String city = map.get("city").toString();
        String dispatch = map.get("dispatch").toString();
        String phone = map.get("phone").toString();
        String scope = map.get("scope").toString();
        Mission mission = new Mission();
        String uuId = IdUtil.randomUUID().replace("-", "");
        mission.setMissionId(uuId);
        mission.setTitle(title);
        mission.setBeginTime(DateUtil.parse(begin));
        mission.setEndTime(DateUtil.parse(end));
        mission.setPlace(place);
        mission.setScene(scene);
        mission.setGroupCode(qrcode);
        mission.setCity(city);
        mission.setDispatch(Integer.parseInt(dispatch));
        mission.setPhone(phone);
        mission.setScope(scope);
        mission.setCreateDate(new Date());
        mission.setStatus(0);
        missionMapper.addMission(mission);
        return ResultUtils.success("OK");
    }

    @Override
    public MsgResponse getMissionPage(MissionPageParam missionPageParam) {
        PageHelper.startPage(missionPageParam.getPageNo(),missionPageParam.getPageSize());
        List<MissionRequestDTO> missionPage = missionMapper.getMissionPage(missionPageParam.getParam());
        PageInfo<MissionRequestDTO> pageInfo = new PageInfo<>(missionPage);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }

    @Override
    public MsgResponse getMissionInfo(Map<String, Object> map) {
        String id = (String) map.get("id");
        Mission mission = missionMapper.selectByPrimaryKey(id);
        return ResultUtils.success(mission);
    }

    @Override
    public MsgResponse editMission(Map<String, Object> map) {
        String id = (String) map.get("id");
        String title = map.get("title").toString();
        String begin = map.get("begin").toString();
        String end = map.get("end").toString();
        String place = map.get("place").toString();
        String scene = map.get("scene").toString();
        String qrcode = map.get("qrcode").toString();
        String city = map.get("city").toString();
        String dispatch = map.get("dispatch").toString();
        String phone = map.get("phone").toString();
        String scope = map.get("scope").toString();
        Mission mission = missionMapper.selectByPrimaryKey(id);
        mission.setBeginTime(DateUtil.parse(begin));
        mission.setTitle(title);
        mission.setEndTime(DateUtil.parse(end));
        mission.setPlace(place);
        mission.setScene(scene);
        mission.setGroupCode(qrcode);
        mission.setCity(city);
        mission.setDispatch(Integer.parseInt(dispatch));
        mission.setPhone(phone);
        mission.setScope(scope);
        missionMapper.editMission(mission);
        return ResultUtils.success("OK");
    }

    @Override
    public MsgResponse close(Map<String, Object> map) {
        String id = (String) map.get("id");
        Mission mission = missionMapper.selectByPrimaryKey(id);
        mission.setStatus(1);
        missionMapper.editMission(mission);
        return ResultUtils.success("OK");
    }


    @Override
    public MsgResponse getApplyPage(MissionDto missionDto) {
        PageHelper.startPage(missionDto.getPageNo(), missionDto.getPageSize());
        List<ApplyDTO> pageList = missionMapper.getApplyList(missionDto.getParam());
        PageInfo<ApplyDTO> pageInfo = new PageInfo<>(pageList);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("count",  pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return ResultUtils.success(resultMap);
    }
}
